package cn.ekuma.epos.catalog;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import cn.ekuma.epos.bean.util.ProductInfoHelper;
import com.openbravo.bean.Product;
import com.openbravo.bean.Tax;
import com.openbravo.data.loader.ImageUtils;
import com.openbravo.pos.bean.ProductExt;
import com.openbravo.pos.catalog.JCatalogTab;
import com.openbravo.pos.panels.event.ProductChangedListener;
import com.openbravo.pos.util.ThumbNailBuilder;

public class JProductListViewPanel extends JPanel {

	private JCatalogTab productList;
	CatelogManager manager;
	
	 private ThumbNailBuilder tnbbutton;
	 
	/**
	 * Create the panel.
	 */
	public JProductListViewPanel(CatelogManager manager) {
        this.manager=manager;
		initComponents();
		tnbbutton = new ThumbNailBuilder(manager.getPicWidth(),manager.getPicHeight(), "com/openbravo/images/package.png");
	}
	
	private void initComponents() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		panel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		
		JButton btnNewButton = new JButton("按名称");
		panel.add(btnNewButton);
		
		JButton button = new JButton("按价格");
		panel.add(button);
		productList = new JCatalogTab();     
		applyComponentOrientation(getComponentOrientation());
        add(productList, BorderLayout.CENTER);
	}
	
	public void reflush(){
		if(productList!=null)
			this.remove(productList);
		productList = new JCatalogTab();     
		applyComponentOrientation(getComponentOrientation());
        add(productList, BorderLayout.CENTER);
           // Add products
       for (ProductExt prod : manager.getProducts()) {
              productList.addButton(new ImageIcon(tnbbutton.getThumbNailText(ImageUtils.readImage(prod.getImage()), getProductLabel(prod))), new SelectedAction(prod));
       }
       validate();
	}
	
	 private class SelectedAction implements ActionListener {
	        private ProductExt prod;
	        public SelectedAction(ProductExt prod) {
	            this.prod = prod;
	        }
	        public void actionPerformed(ActionEvent e) {
	        	fireProductChanged(prod);
	        }
	    }
	
	
	public void  addProductChangedListener(ProductChangedListener l){
		listenerList.add(ProductChangedListener.class, l);
	}
	
	public void removeProductChangedListener(ProductChangedListener l){
		listenerList.remove(ProductChangedListener.class, l);
	}
	
	public void fireProductChanged(Product obj){
		for(ProductChangedListener l:listenerList.getListeners(ProductChangedListener.class))
			l.baseBeanChanged(obj, obj);	 
	}
	
	 private String getProductLabel(ProductExt product) {
	        if (manager.isPricevisible()) {
	            if (manager.isTaxesincluded()) {
	                Tax tax = manager.getTaxInfo(product.getTaxCategoryID());
	                return "<html><center>" + product.getName() + "<br>" + ProductInfoHelper.printPriceSellTax(product, tax);
	            } else {
	                return "<html><center>" + product.getName() + "<br>" + ProductInfoHelper.printPriceSell(product);
	            }
	        } else {
	            return product.getName();
	        }
	    }
}
